#ifndef SHENLAN_BLUR
#define SHENLAN_BLUR
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"


half4 SampleGaussian3x3(Texture2D tex_, SamplerState sam_, float2 uv_, float2 scale_ = 1)
{
    half3 o = half3(1, -1, 0);
    o.xy *= scale_;
    half4 c = SAMPLE_TEXTURE2D(tex_, sam_, uv_) * 4;

    c += SAMPLE_TEXTURE2D(tex_, sam_, uv_ + o.yz) * 2; //L
    c += SAMPLE_TEXTURE2D(tex_, sam_, uv_ + o.xz) * 2; //R
    c += SAMPLE_TEXTURE2D(tex_, sam_, uv_ + o.zx) * 2; //T
    c += SAMPLE_TEXTURE2D(tex_, sam_, uv_ + o.zy) * 2; //B

    c += SAMPLE_TEXTURE2D(tex_, sam_, uv_ + o.yx); //LT
    c += SAMPLE_TEXTURE2D(tex_, sam_, uv_ + o.xx); //RT
    c += SAMPLE_TEXTURE2D(tex_, sam_, uv_ + o.yy); //LB
    c += SAMPLE_TEXTURE2D(tex_, sam_, uv_ + o.xy); //RB

    return c * 0.0625f;
}

//5x5 方差 = 1 的 标准高斯核分解
half4 SampleGaussian5x5_Split(Texture2D tex_, SamplerState sam_, float2 uv_, half2 dir_,float2 scale_ = 1)
{
    half2 o = dir_ * scale_;
    float3 kernal = float3(0.4026f,0.2442f,0.0545f);
    
    half4 c = SAMPLE_TEXTURE2D(tex_,sam_,uv_) * kernal.x;

    c+= SAMPLE_TEXTURE2D(tex_,sam_,uv_ + o * 1) * kernal.y;
    c+= SAMPLE_TEXTURE2D(tex_,sam_,uv_ + o * 2) * kernal.z;
    c+= SAMPLE_TEXTURE2D(tex_,sam_,uv_ - o * 1) * kernal.y;
    c+= SAMPLE_TEXTURE2D(tex_,sam_,uv_ - o * 2) * kernal.z;

    return c;    
}

half4 SampleGaussian5x5_Horizontal(Texture2D tex_, SamplerState sam_, float2 uv_, float2 scale_ = 1)
{
    return SampleGaussian5x5_Split(tex_,sam_,uv_,half2(1,0),scale_);
}

half4 SampleGaussian5x5_Vertical(Texture2D tex_, SamplerState sam_, float2 uv_, float2 scale_ = 1)
{
    return SampleGaussian5x5_Split(tex_,sam_,uv_,half2(0,1),scale_);
}



half4 SampleBox2x2(Texture2D tex_, SamplerState sam_, float2 uv_, float2 scale_ = 1)
{
    half3 o = half3(1, -1, 0);
    o.xy *= scale_;
    half4 c = 0;
    c += SAMPLE_TEXTURE2D(tex_, sam_, uv_ + o.yx); //LT
    c += SAMPLE_TEXTURE2D(tex_, sam_, uv_ + o.xx); //RT
    c += SAMPLE_TEXTURE2D(tex_, sam_, uv_ + o.yy); //LB
    c += SAMPLE_TEXTURE2D(tex_, sam_, uv_ + o.xy); //RB

    return c * 0.25f;
}

#endif
